import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * @file MarkList.java
 * @author zemo
 * @mail zemochen@gmail.com
 * @data 2014年4月22日 下午11:15:18
 * @description: 书签操作类
 */
public class MarkList extends List implements CommandListener{
	private InfoCanvas infoCanvas;
	private Display display;
	
	private Command cmdRead = new Command("读取",Command.EXIT,1);
	private Command cmdReturn = new Command("返回",Command.ITEM,1);
	private Command cmdDelete = new Command("删除",Command.ITEM,1);
	
	private int screen;
	private int wordColorSelect,BGColorSelect; //字体及背景颜色选择项
	private int fontSizeSelect,fontStyleSelect,fontFaceSelect;//字体样式,大小等选项
	
	public MarkList(InfoCanvas mainScreen,Display display){
		super("书签管理",List.EXCLUSIVE);
		this.infoCanvas = mainScreen;
		this.display = display;
		this.addCommand(cmdRead);
		this.addCommand(cmdReturn);
		this.addCommand(cmdDelete);
		this.setCommandListener(this);
		this.deleteAll();
		init();
	}
	public void init(){
		try{
			RecordStore rmsmark = RecordStore.openRecordStore("rms", true);
			if(rmsmark!=null){
				int numRecord = rmsmark.getNumRecords();
				for(int i=1;i<numRecord+1;i++){
					byte []data = rmsmark.getRecord(i);
					if(data!=null){
						StringBuffer sb = new StringBuffer();
						String result = sb.append(new String(data)).toString();
						System.out.println(result);
						if(!result.equalsIgnoreCase("false")){
							screen = Integer.parseInt(result.substring(0, 1));
							this.append(i+"."+infoCanvas.getMarkString(screen), null);
						}
					}
				}
			}
			rmsmark.closeRecordStore();
		}catch (RecordStoreException rse) {
            System.err.println(rse.toString());
        }
	}
	public void commandAction(Command c, Displayable d) {
		// TODO Auto-generated method stub
		if(c == cmdReturn){
			display.setCurrent(infoCanvas);
		}else if(c == cmdRead){
			int index;
			try{
				RecordStore rmsmark = RecordStore.openRecordStore("rms", true);
				System.out.println(this.getString(this.getSelectedIndex()));
				String list = this.getString(this.getSelectedIndex());
				index =Integer.parseInt(list.substring(0,list.indexOf(".")));
				byte[]data1 = rmsmark.getRecord(index);
				StringBuffer sb1 = new StringBuffer();
				String result1 = sb1.append(new String(data1)).toString();
				System.out.println(result1.substring(0, 1));
				screen = Integer.parseInt(result1.substring(0, 1));
				wordColorSelect = Integer.parseInt(result1.substring(1, 2));
				BGColorSelect = Integer.parseInt(result1.substring(2, 3));
				rmsmark.closeRecordStore();
				infoCanvas.setScreen(screen);
				infoCanvas.setColor(wordColorSelect, BGColorSelect);
				display.setCurrent(infoCanvas);
				
			}catch (RecordStoreException rse) {
	            System.err.println(rse.toString());
	        }
		}else if(c == cmdDelete){
			Alert alert = new Alert("删除书签","是否真得删除该书签",null,AlertType.ERROR);
			alert.setTimeout(Alert.FOREVER);
			display.setCurrent(alert);
			//this.delete(this.getSelectedIndex());
			
			
			try{
				RecordStore rmsmark = RecordStore.openRecordStore("rms", true);
				String str="false";
				byte[]data=str.getBytes();
				String selected = this.getString(this.getSelectedIndex());
				int rcId = Integer.parseInt(selected.substring(0, selected.indexOf(".")));
				//System.out.println(selected.substring(0, selected.indexOf(".")));
				
				rmsmark.deleteRecord(this.getSelectedIndex());
				//rmsmark.setRecord(this.getSelectedIndex()+1,data,0,data.length);
				rmsmark.closeRecordStore();
				deleteAll();
				init();
			}catch (RecordStoreException rse) {
	            System.err.println(rse.toString());
	        }
		}
	}
}
